package softtech.app.secretcaller;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterSmsList extends ArrayAdapter<DatabaseContact> {

	List<DatabaseContact> mListContact;
	Context mContext;
	
	DatabaseHandler db;
	
	private int mode_load;
	
	public AdapterSmsList(Context ctx ,  int textViewResourceId , ArrayList<DatabaseContact> list){
		super(ctx, textViewResourceId);
		db = new DatabaseHandler(ctx);
		mListContact = list;
		mContext = ctx;
		mode_load = 1;
	}

	public int getMode_load() {
		return mode_load;
	}

	public void setMode_load(int mode_load) {
		this.mode_load = mode_load;
	}

	@Override
	public int getCount() {
		if(mListContact==null || mListContact.equals(null) || mListContact.size() == 0){
			return 1;
		}
		
		return mListContact.size();
	}

	@Override
	public DatabaseContact getItem(int position) {
		if(mListContact==null || mListContact.equals(null) || mListContact.size() == 0){
			return null;
		}
		return mListContact.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(parent.getContext()==null){		
			return null;
		}
		
		if(mListContact.size()==0){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_search_notfound,parent, false);
			return convertView;
		}
		
		ViewHolder viewHolder=null;
		if(convertView==null){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.layout_sms_item,parent, false);
			
			viewHolder=new ViewHolder();
			viewHolder.tvNameContact	= (TextView) convertView.findViewById(R.id.tvNameContact);
			viewHolder.tvContentSms = (TextView) convertView.findViewById(R.id.tvContentSms);
			viewHolder.imgTypeAvatar	= (ImageView) convertView.findViewById(R.id.imgTypeAvatar);
			viewHolder.imgDelete			= (ImageView) convertView.findViewById(R.id.imgDelete);
			viewHolder.tvTimeOfSms 	= (TextView) convertView.findViewById(R.id.tvTimeOfSms);
						
			convertView.setTag(viewHolder);
			convertView.setTag(R.id.tvNameContact,viewHolder.tvNameContact);
			convertView.setTag(R.id.tvContentSms,viewHolder.tvContentSms);
			convertView.setTag(R.id.imgTypeAvatar , viewHolder.imgTypeAvatar );
			convertView.setTag(R.id.imgDelete, viewHolder.imgDelete);
			convertView.setTag(R.id.tvTimeOfSms , viewHolder.tvTimeOfSms);
			
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		
		viewHolder.tvNameContact.setText(mListContact.get(position).getContactName());
		if(mode_load==1){
			viewHolder.tvContentSms.setText(mListContact.get(position).getLastCall());
		}else{
			
			
			try{
				if(mListContact.get(position).getLastSms().length() >40){
					viewHolder.tvContentSms.setText(mListContact.get(position).getLastSms().substring(19 , 35) + "...");
				}else{
					viewHolder.tvContentSms.setText(mListContact.get(position).getLastSms().substring(19));
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			
			viewHolder.tvTimeOfSms.setText(mListContact.get(position).getLastSms().substring(5, 10) + "\n" + 
					mListContact.get(position).getLastSms().substring(11,16));
			
			
			
		}
		
		
//		viewHolder.imgDelete.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if(mode_load==1){
//					mListContact.get(position).setLastCall("");
//					db.updateContact(mListContact.get(position));
//					mListContact.remove(position);
//					notifyDataSetChanged();
//				}else{
//					mListContact.get(position).setLastSms("");
//				}
//				
//				
//			}
//		});
		
		return convertView;
	}

	static class ViewHolder{
		TextView tvNameContact , tvContentSms;
		ImageView imgTypeAvatar;
		ImageView imgDelete;
		TextView tvTimeOfSms;
	}
	
	
}
